#!/usr/bin/env bash
set -euo pipefail

HOST=${HOST:-${MQTT_HOST:-broker.emqx.io}}
PORT=${PORT:-${MQTT_PORT:-1883}}
TOPIC=${TOPIC:-${MQTT_TOPIC:-756687933516/AIOTSIM2Dev}}

if ! command -v mosquitto_pub >/dev/null 2>&1; then
  echo "mosquitto_pub not found. Install: sudo apt install -y mosquitto-clients" >&2
  exit 1
fi

echo "Publish examples to $HOST:$PORT topic=$TOPIC"
echo "1) Turn lamp on (id=0)"
mosquitto_pub -h "$HOST" -p "$PORT" -t "$TOPIC" -m '{"lamp":true,"id":0}'
sleep 1
echo "2) Turn lamp off (id=0)"
mosquitto_pub -h "$HOST" -p "$PORT" -t "$TOPIC" -m '{"lamp":false,"id":0}'
sleep 1
echo "3) Fan on (id=0)"
mosquitto_pub -h "$HOST" -p "$PORT" -t "$TOPIC" -m '{"fan":true,"id":0}'
sleep 1
echo "4) Fan 60% (id=0)"
mosquitto_pub -h "$HOST" -p "$PORT" -t "$TOPIC" -m '{"fan":60,"id":0}'

